home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / gnushogi.lha / gnushogi-1.1 / src / ataks.h < prev    next >
C/C++ Source or Header  |  1993-04-16  |  3KB  |  148 lines

  1. /*
  2.  * ataks.h - Header source for GNU SHOGI
  3.  *
  4.  * Copyright (c) 1993 Matthias Mutz
  5.  *
  6.  * GNU SHOGI is based on GNU CHESS
  7.  *
  8.  * Copyright (c) 1988,1989,1990 John Stanback
  9.  * Copyright (c) 1992 Free Software Foundation
  10.  *
  11.  * This file is part of GNU SHOGI.
  12.  *
  13.  * GNU Shogi is free software; you can redistribute it and/or modify
  14.  * it under the terms of the GNU General Public License as published by
  15.  * the Free Software Foundation.
  16.  *
  17.  * GNU Shogi is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with GNU Shogi; see the file COPYING.  If not, write to
  24.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  */
  26.  
  27.  
  28. #define CHECK_DISTANCE
  29.  
  30.  
  31. inline
  32. static
  33. int
  34. SqAtakd (register short int square, short int side)
  35.  
  36. /*
  37.  * See if any piece with color 'side' ataks sq.  
  38.  */
  39.  
  40. {
  41.   register unsigned char *ppos, *pdir;
  42.   register short u, ptyp;
  43.  
  44.  /*
  45.   * First check distance 1,
  46.   * then check Bishops,
  47.   * then check Rooks,
  48.   * last check Knights.
  49.   */
  50.  
  51.   /* try a capture from distance 1 */
  52.  
  53.   ptyp = ptype[black][king];
  54.   pdir = (*nextdir[ptyp])[square];
  55.   u = pdir[square];
  56.   do
  57.     {
  58.       if (color[u] == side)
  59.     /* can piece reach square in one step ? */
  60. #ifdef CHECK_DISTANCE
  61.         if ( piece_distance(side,board[u],u,square) == 1 )
  62.       return(true);
  63. #else   
  64.     {
  65.       short v;
  66.       unsigned char *qdir;
  67.       qdir = (*nextdir[ptype[side][board[u]]])[u];
  68.       v = qdir[u];
  69.       do
  70.         {
  71.           if (v == square)
  72.         return (true);
  73.           v = qdir[v];
  74.       } while (v != u);
  75.     }
  76. #endif
  77.       u = pdir[u];
  78.   } while (u != square);
  79.  
  80.   /* try a (promoted) bishop capture */
  81.  
  82.   ptyp = ptype[black][bishop];
  83.   ppos = (*nextpos[ptyp])[square];
  84.   pdir = (*nextdir[ptyp])[square];
  85.   u = ppos[square];
  86.   do
  87.     {
  88.       if (color[u] == neutral)
  89.     u = ppos[u];
  90.       else
  91.     {
  92.       if (color[u] == side && (unpromoted[board[u]] == bishop))
  93.         return (true);
  94.       u = pdir[u];
  95.     }
  96.   } while (u != square);
  97.  
  98.   /* try a (promoted) rook capture */
  99.  
  100.   ptyp = ptype[black][rook];
  101.   ppos = (*nextpos[ptyp])[square];
  102.   pdir = (*nextdir[ptyp])[square];
  103.   u = ppos[square];
  104.   do
  105.     {
  106.       if (color[u] == neutral)
  107.     u = ppos[u];
  108.       else
  109.     {
  110.       if (color[u] == side && (unpromoted[board[u]] == rook))
  111.         return (true);
  112.       u = pdir[u];
  113.     }
  114.   } while (u != square);
  115.  
  116.   /* try a lance capture (using xside's lance moves) */
  117.  
  118.   ptyp = ptype[side ^ 1][lance];
  119.   ppos = (*nextpos[ptyp])[square];
  120.   u = ppos[square];
  121.   do
  122.     {
  123.       if (color[u] == neutral)
  124.     u = ppos[u];
  125.       else
  126.     {
  127.       if (color[u] == side && (board[u] == lance))
  128.         return (true);
  129.       u = square;
  130.     }
  131.   } while (u != square);
  132.  
  133.   /* try a knight capture (using xside's knight moves) */
  134.  
  135.   ptyp = ptype[side ^ 1][knight];
  136.   pdir = (*nextdir[ptyp])[square];
  137.   u = pdir[square];
  138.   do
  139.     {
  140.       if (color[u] == side && board[u] == knight)
  141.     return (true);
  142.       u = pdir[u];
  143.   } while (u != square);
  144.  
  145.   return (false);
  146.  
  147. }
  148.